package leetcode;

import java.util.Stack;

/**
 * 最小栈
 */
public class MinStack {
    Stack<Integer> stack1 = new Stack<>();

    Stack<Integer> stack2 = new Stack<>();

    public MinStack() {}

    public void push(int val) {
        stack1.push(val);
        // 同步更新辅助栈中的元素
        if (stack2.isEmpty()) {
            stack2.add(val);
        } else {
            if (stack2.peek() >= val) {
                stack2.add(val);
            } else {
                stack2.add(stack2.peek());
            }
        }
    }

    public void pop() {
        stack1.pop();
        stack2.pop();
    }

    public int top() {
        return stack1.peek();
    }

    public int getMin() {
        return stack2.peek();
    }
}
